import { lists } from "@/views/kitchen/index.json";


// 获取当前时间作为订购时间
const getTime = function () {
  const now = new Date();
  const year = now.getFullYear();
  const month = ('0' + (now.getMonth() + 1)).slice(-2);
  const day = ('0' + now.getDate()).slice(-2);
  const hours = ('0' + now.getHours()).slice(-2);
  const minutes = ('0' + now.getMinutes()).slice(-2);
  const seconds = ('0' + now.getSeconds()).slice(-2);
  const formattedTime = year + '-' + month + '-' + day + ' ' + hours + ':' + minutes + ':' + seconds;
  const fullTime = year + month + day
  return {
    fullTime,
    formattedTime
  }
}


const state = {
  kitchenList: JSON.parse(localStorage.getItem('getKitchenList')) || lists
}

const mutations = {
  changeState(state, data) {
    let list = state.kitchenList
    let newList = list.map(item => {
      item.children.map(items => {
        if (items.name == data.name) {
          items = data
          // 新增订购时间
          items.time = getTime().formattedTime;
          // 新增当前时间戳
          items.orders = 'cdj' + getTime().fullTime + Date.parse(new Date())
        }
        return items
      })
      return item
    })
    localStorage.setItem("getKitchenList", JSON.stringify(newList))
  }
}

const actions = {}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}